//
//  HZGoodsDetailTopModel.m
//  app
//
//  Created by 刘华舟 on 15/6/19.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZGoodsDetailTopModel.h"
#import "NSString+Conveniently.h"
@implementation HZGoodsDetailTopModel

+ (instancetype)modelWithDictionary:(NSDictionary *)dict{
    return [[HZGoodsDetailTopModel alloc] initModelWithDictionary:dict];
}

- (instancetype)initModelWithDictionary:(NSDictionary *)dict{
    if (self = [super init]) {
        _shopID = [dict stringForKey:APIKEY_mall_shop_id nilValue:@"0"];
        _shopName = [dict stringForKey:APIKEY_mall_shop_name nilValue:@""];
        _productID = [dict stringForKey:APIKEY_mall_product_id nilValue:@"0"];
        _productTitle = [dict stringForKey:APIKEY_mall_product_title nilValue:@""];
        _productName = [dict stringForKey:APIKEY_mall_product_name nilValue:@""];
        _productIntros = [dict stringForKey:APIKEY_mall_product_intros nilValue:@""];
        _tagPrice = [dict stringForKey:APIKEY_mall_tag_price nilValue:@"0"];
        _averagePrice = [dict stringForKey:APIKEY_mall_average_prices nilValue:@"0"];
        _salePrice = [dict stringForKey:APIKEY_mall_sale_price nilValue:@"0"];
        _giftGlods = [dict stringForKey:APIKEY_mall_giftgolds nilValue:@"0"];
        _jifen = [dict stringForKey:APIKEY_mall_jifen nilValue:@"0"];
        _gold = [dict stringForKey:APIKEY_mall_gold nilValue:@"0"];
        _saleNum = [dict stringForKey:APIKEY_mall_sale_num nilValue:@"0"];
        _num = [dict stringForKey:APIKEY_mall_num nilValue:@"9999"];
        _extraTitle1 = [dict stringForKey:APIKEY_mall_extra_title1 nilValue:@""];
        _extraTitle2 = [dict stringForKey:APIKEY_mall_extra_title2 nilValue:@""];
        _productPic = [dict stringForKey:APIKEY_mall_pic nilValue:@""];
        _width = [dict stringForKey:APIKEY_mall_width nilValue:@"1"];
        _height = [dict stringForKey:APIKEY_mall_height nilValue:@"1"];
        
        _goldInfo = [dict dictForKey:APIKEY_mall_goldInfo];
        _gold2Money = [_goldInfo stringForKey:APIKEY_mall_gold2money nilValue:@"0"];
        _gold2RMB = [_goldInfo stringForKey:APIKEY_mall_1gold2RMB nilValue:@"0"];
        
        _productType = [dict stringForKey:APIKEY_mall_product_type nilValue:@"normal"]; //默认是正常商品
        
        _srvStartTime = [dict stringForKey:APIKEY_mall_starttime nilValue:@"0"]; //默认无起售时间
        _srvEndTime = [dict stringForKey:APIKEY_mall_endtime nilValue:@"0"];    //默认无结束时间
        
        
        _productPictures = [dict arrayForKey:APIKEY_mall_product_picture];
        
        /** 计算下发图片的高度 -write by khzliu */
        _topPictureHeight = 0.0f;
        if (_productPictures.count > 0) {
            NSDictionary *picDict = [_productPictures firstObject];
            if (picDict != nil && [picDict isKindOfClass:[NSDictionary class]]) {
                CGFloat width = [[picDict stringForKey:APIKEY_mall_width nilValue:@"0"] floatValue];
                CGFloat height = [[picDict stringForKey:APIKEY_mall_height nilValue:@"0"] floatValue];
                
                if (width > 0 && height > 0) {
                    _topPictureHeight = [UBO_SCREEN fw:UB_WIDTH]*height/width;
                }
            }
            
        }
        
        _specifications = [dict arrayForKey:APIKEY_mall_specifications];
        _specificationsAllHeight = 0;
        NSMutableArray *cellHeightArray = [NSMutableArray array];
        for (NSDictionary *dict in _specifications) {
            /** 计算文字高度 -write by khzliu */
            NSString *values = [dict stringForKey:APIKEY_mall_val nilValue:@""];
            CGFloat cellHeight = [NSString heightForString:values font:JKAppGlobleFontSmallTagText width:[UBO_SCREEN.ubo fw:UB_WIDTH uw:-5.0/2*kSizeMallViewMargin ow:-160/2] maxHeight:9999.9f];
            if (cellHeight+kSizeMallViewMargin<36.0f) {
                cellHeight = 36.0f;
            }else{
                cellHeight = cellHeight+kSizeMallViewMargin;
            }
            [cellHeightArray addObject:[NSString stringWithFormat:@"%f", cellHeight]];
            _specificationsAllHeight += cellHeight;
        }
        _specificationsCellHeightArray = [NSArray arrayWithArray:cellHeightArray];
        
//        //----------------------------khzliu test start-----------------------------
//        _specifications = @[@{APIKEY_mall_name:@"产品名称",APIKEY_mall_val:@"汤臣倍健蛋白质粉455g/罐"},@{APIKEY_mall_name:@"营养成分",APIKEY_mall_val:@"每100克含蛋白质80g"},@{APIKEY_mall_name:@"方法用量",APIKEY_mall_val:@"每日10克，可加入牛奶，果汁及谷物，蔬菜等食品中食用"},@{APIKEY_mall_name:@"产品规格",APIKEY_mall_val:@"早餐或午餐，餐后服务（水温<50度）"},@{APIKEY_mall_name:@"批准文号",APIKEY_mall_val:@"国食健字G20050485"}];
//        //----------------------------khzliu test end-----------------------------

        
        NSMutableArray *array = [NSMutableArray array];
        for (NSDictionary *cell in [dict arrayForKey:APIKEY_mall_extend_intros]) {
            HZProductTextImageDetailModel *model = [HZProductTextImageDetailModel modelWithDictionary:cell];
            [array addObject:model];
        }
        _extendIntors = array;
        
        _isHot = [dict stringForKey:APIKEY_mall_isHot nilValue:@"0"];
        
        _collection = NO;
        
        _pinglunNum = [dict stringForKey:APIKEY_mall_pinglun_num nilValue:@"0"];
        
        if (_extraTitle1.length) {
            
            _viewHeight = [UBO_SCREEN uh:kPicHeadViewHeight+kSizeMallViewMargin/2] + _topPictureHeight;
        }else{
            _viewHeight = [UBO_SCREEN uh:kPicHeadViewHeight] + _topPictureHeight;
        }
    
        _buyNum = 1;
        
        _openTime = [dict stringForKey:@"open_time" nilValue:@"09:30-23:59"];
        
        _deliveryURL = [dict stringForKey:APIKEY_mall_deliveryURL nilValue:@""];
        
        
        _promotion = [dict arrayForKey:APIKEY_mall_promotion];
    }
    return self;
}


- (void)setBuyNum:(NSInteger)buyNum{
    if (buyNum <= 0) {
        _buyNum = 1;
    }else{
        _buyNum = buyNum;
    }
}

@end
